﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Zxb.DayWork.Logic;

namespace Zxb.OneTimeWork
{
    public class statReport
    {
        private DateTime enddate;
        private DateTime startdate;
        private ZxConstant zxconstant;

        DataTable caseDt;
        DataTable promotionDt;
        DataTable refreshDt;
        DataTable reserveDt;

        public statReport()
        {
            startdate = DateTime.Now;
            enddate = startdate.AddDays(1.0);
            zxconstant = ZxConstantList.GetZxConstant(ZxJc.All);
        }
        public statReport(DateTime start,DateTime end,ZxJc z)
        {
            startdate = start;
            enddate = end;
            zxconstant = ZxConstantList.GetZxConstant(z);
        }

        public DataTable CaseCount()
        {
            string sql = string.Format("select WeiKe_UserID,COUNT(1) as caseNum from Cases with(nolock) where AddedTime>='{0}' and AddedTime<'{1}' and IsValid=1 {2} group by WeiKe_UserID", startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.whereCase);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        public DataTable PromotionCount()
        {
            string sql = string.Format("select DesignerID,count(1) as proNum from CompanyNews with(nolock) where PublishDate>='{0}' and PublishDate<'{1}' and IsDel=0 {2} group by DesignerID"
            , startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.wherePro);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        public DataTable RefreshCount()
        {
            string sql = string.Format("select OperateUserID,COUNT(1) as refreshNum from opLog with(nolock) where RefreshTime>='{0}' and RefreshTime<'{1}' {2} group by OperateUserID"
            , startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.whereRefresh);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.ZxbReader, sql);
        }

        public DataTable ReserveCount()
        {
            string sql = string.Format("select UserID,COUNT(1) as reserveNum from RefreshReserve with(nolock) where [time]>='{0}' and [time]<'{1}' {2} group by UserID "
            , startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.whereReserve);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.ZxbReader, sql);
        }

        public void Run()
        {
            caseDt = CaseCount();
            promotionDt = PromotionCount();
            refreshDt = RefreshCount();
            reserveDt = ReserveCount();
        }
    }

    public enum ZxJc
    {
        ZhuangXiu = 1,
        JianCai = 2,
        All = 3
    }

    public class ZxConstant
    {
        public ZxJc zxtype;
        public string whereCase;
        public string wherePro;
        public string whereRefresh;
        public string whereReserve;
    }
    public class ZxConstantList:List<ZxConstant>
    {
        private static List<ZxConstant> list = new List<ZxConstant>();

        public static ZxConstant GetZxConstant(ZxJc z)
        {
            ZxConstant result = null;
            foreach (ZxConstant c in list)
            {
                if (c.zxtype == z)
                {
                    result = c;
                    break;
                }
            }
            return result;
        }

        static ZxConstantList()
        {
            list.Add(new ZxConstant() { zxtype = ZxJc.ZhuangXiu, whereCase = "and WeiKe_UserID>0", wherePro = "and DesignerID>0", whereRefresh = "and OperateUserID<450000", whereReserve = "and channel=2" });
            list.Add(new ZxConstant() { zxtype = ZxJc.JianCai, whereCase = "and WeiKe_UserID<0", wherePro = "and DesignerID<0", whereRefresh = "and OperateUserID>450000", whereReserve = "and channel=1" });
            list.Add(new ZxConstant() { zxtype = ZxJc.All, whereCase = "", wherePro = "", whereRefresh = "",whereReserve="" });
        }
    }

}
